<?php
// settings include
include ("../config.php");

//page def
//the page title
$title = "Import Recipe";

// dB connection
include ("$install_dir/include/db.php");

// functions
function addto ($rec_name, $rec_source, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_qm, $rec_ingred, $rec_direct, $rec_notes) {
global $install_url, $install_dir, $pear_dir, $dB_host, $dB_user, $dB_pw, $dB_dB;

?>
<form name="recipes" method="POST" action="<?php echo $install_url;?>/admin/add.php?mode=add">
   <table cellpadding="0" cellspacing="0">
   <tr>
   <td width="25%">
   Recipe Name:
   </td>
   <td width="75%">
   <input type="text" name="rec_name" value="<?php echo $rec_name;?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Source / By:
   </td>
   <td width="75%">
   <input type="text" name="rec_source" value="<?php echo $rec_source;?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Catigory:
   </td>
   <td width="75%">
   <input type="text" name="rec_cat" value="<?php echo $rec_cat;?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Main Ingredient:
   </td>
   <td width="75%">
   <input type="text" name="rec_main" value="<?php echo $rec_main;?>" size="35" maxlength="128">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Prep-Time:
   </td>
   <td width="75%">
   <input type="text" name="rec_prep" value="<?php echo $rec_prep;?>" size="35" maxlength="64">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Cook-Time:
   </td>
   <td width="75%">
   <input type="text" name="rec_cook" value="<?php echo $rec_cook;?>" size="35" maxlength="64">
   </td>
   </tr>
   <tr>
   <td width="25%">
   Servings:
   </td>
   <td width="75%">
   <input type="text" name="rec_serves" value="<?php echo $rec_serves;?>" size="35" maxlength="64">
   </td>
   </tr>
   <td width="25%">
   Quick Meal:
   </td>
   <td width="75%">
   <input type="radio" name="rec_qm" <?php if($rec_qm=="1")echo "checked ";?> value="1">Yes <input type="radio" name="rec_qm" <?php if($rec_qm=="0")echo "checked ";?> value="0">No 
   </td>
   </tr>
   <tr>
   <td width="25%">
   Ingredients:
   </td>
   <td width="75%">
   <textarea name="rec_ingred" rows="5" cols="50"><?php echo $rec_ingred; ?></textarea><br />
   <input type="button" value="Help: BBCode" onClick="help_bbc();"/>
   </td>
   </tr>
   <tr>
   <td width="25%">
   Directions:
   </td>
   <td width="75%">
   <textarea name="rec_direct" rows="5" cols="50"><?php echo $rec_direct; ?></textarea><br />
   <input type="button" value="Help: BBCode" onClick="help_bbc();"/>
   </td>
   </tr>
   <tr>
   <td width="25%">
   Notes:
   </td>
   <td width="75%">
   <textarea name="rec_notes" rows="5" cols="50"><?php echo $rec_notes; ?></textarea><br />
   <input type="button" value="Help: BBCode" onClick="help_bbc();"/>
   </td>
   </tr>
   <tr>
   <td>
   </td>
   <td>
   <input type="submit" value="Submit"><input type="reset" value="Reset"><input type="button" value="Check Spelling" onClick="openSpellChecker();"/>
   </td>
   </tr>
   </table>
</form>
<?php
	 return;
}

//adds the header
include ("$install_dir/include/head.php");

//start page
?>
<body>
<center>
<table class="main_table">
<tr>
<th colspan="2" class="logo"><img src="<?php echo $install_url;?>/images/logo.png" alt="My Personal Cookbook" align="left" /></th>
</tr>
<tr>
<td class="menu">
<?php
//adds menu
include ("$install_dir/include/menu.php");

?>
</td>
<td class="page">
<div class = "title">:. <?php echo $title;?> .:</div>
<div class = "post_text">
<?php

$rec_type = $_POST['rec_type'];
$rec_import = $_POST['rec_import'];

if ($mode == NULL) {
   ?>
   <form name="test" method="POST" action="<?php echo $install_url;?>/admin/import.php?mode=work">
   		 <table cellpadding="0" cellspacing="0">
		 <td width="25%">
   		 Recipe:
   		 </td>
		 <td width="75%">
   		 <textarea name="rec_import" rows="20" cols="50"></textarea><br />
   		 <input type="submit" value="Submit">
		 </td>
		 </tr>
		 <td width="25%">
   		 Select Program, Version, Header:
   		 </td>
   		 <td width="75%">
   		 <input type="radio" name="rec_type" value="MM804">MMMMM----- Recipe via Meal-Master (tm) v8.04 <br />
		 <input type="radio" name="rec_type" value="MM805">---------- Recipe via Meal-Master (tm) v8.05 <br />
   		 </td>
   		 </tr>
		 </table>
   </form>
   <?php
}

elseif ($mode == "work" && $rec_type == "MM804") {
	   $rec_import = str_replace("\'", "'", $rec_import);
	   $rec_import = str_replace('\"', '"', $rec_import);
	   $rec_import = str_replace("\r", NULL, $rec_import);
	   
	   $rec_import = explode("\n \n", $rec_import);
	   $src = str_replace("MMMMM----- ", NULL, $rec_import[0]);
	   $name_cat_ser = $rec_import[1];
	   $ingred = $rec_import[2];
	   $direct =  $rec_import[3];
	   $notes = $rec_import[4];
	   	   
	   $name_cat_ser_exp = explode("\n", $name_cat_ser);  
	   $name = str_replace("      Title: ", NULL , $name_cat_ser_exp[0]);
	   $cat_main = str_replace(" Categories: ", NULL , $name_cat_ser_exp[1]);
	   $serv = str_replace("      Yield: ", NULL , $name_cat_ser_exp[2]);
	   
	   $cat_main = explode(", ", $cat_main);
	   $cat = $cat_main[0];
	   $main = $cat_main[1];
	   
	   $rec_name = $name;
	   $rec_source = $src;
	   $rec_cat = $cat;
	   $rec_main = $main;
	   $rec_prep = NULL;
	   $rec_cook = NULL;
	   $rec_serves = $serv;
	   $rec_qm = NULL;
	   $rec_ingred = $ingred;
	   $rec_direct = $direct;
	   $rec_notes = NULL; //$notes;
	   
	   addto ($rec_name, $rec_source, $rec_cat, $rec_main, $rec_prep, $rec_cook, $rec_serves, $rec_qm, $rec_ingred, $rec_direct, $rec_notes);
}

elseif ($mode == "work" && $rec_type != NULL) {
	   echo "Error: No type specified!";
}
?>